﻿using AutoStar.Common;
using GalaSoft.MvvmLight.CommandWpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using AutoStar.Models;
using SharpGL.WPF;
using SharpGL;
using System.Windows.Media.Imaging;
using System.Reflection;
using System.Windows.Threading;

namespace AutoStar.ViewModel
{
    public class PrintStatusVM : BaseViewModel
    {
        MainWindowVM _mainWindowVM;
        public List<PrinChannelModel> prinChannels { get; set; }
        public ICommand PrintStatusView_Loaded { get; set; }
        public PrintStatusVM()
        {
            PrintStatusView_Loaded = new RelayCommand(PrintStatusView_Loaded_Execute);
            prinChannels = new List<PrinChannelModel>();
        }

        private string _lblX = "0.00";
        public string lblX
        {
            get => _lblX;
            set => Set(ref _lblX, value);
        }
        private string _lblY = "0.00";
        public string lblY
        {
            get => _lblY;
            set => Set(ref _lblY, value);
        }
        private string _lblZ = "0.00";
        public string lblZ
        {
            get => _lblZ;
            set => Set(ref _lblZ, value);
        }
        private string _lblChannel = "0";
        public string lblChannel
        {
            get => _lblChannel;
            set => Set(ref _lblChannel, value);
        }
        private string _lblBattery = "0";
        public string lblBattery
        {
            get => _lblBattery + "（KPa）";
            set => Set(ref _lblBattery, value);
        }
        private string _lblAirPress = "0";
        public string lblAirPress
        {
            get => _lblAirPress + "（℃）";
            set => Set(ref _lblAirPress, value);
        }
        private string _lblPrintSpace = "0";
        public string lblPrintSpace
        {
            get => _lblPrintSpace + "（um）";
            set => Set(ref _lblPrintSpace, value);
        }
        private string _lblStartTime = "0";
        public string lblStartTime
        {
            get => _lblStartTime + "（us）";
            set => Set(ref _lblStartTime, value);
        }
        private string _lblWorkMode = "点喷式";
        public string lblWorkMode
        {
            get => _lblWorkMode;
            set => Set(ref _lblWorkMode, value);
        }

        private OpenGLControl _OpenGLControl1 = new OpenGLControl();
        public OpenGLControl OpenGLControl1
        {
            get => _OpenGLControl1;
            set => Set(ref _OpenGLControl1, value);
        }
        private double _numericValue = 0.00;
        double step = 0.25;
        public double NumericValue
        {
            get => (double)_numericValue;
            //set => Set(ref _numericValue, Math.Round(value / step) * step);
            set
            {
                Set(ref _numericValue, Math.Round(value / step) * step);
                if (_numericValue > 0)
                    Global.printer.moveToAbsolute(Global.printer.X, Global.printer.Y, (float)_numericValue);
            }
        }
        private double _numericValue2 = 60;
        public double NumericValue2
        {
            get => (double)_numericValue2;
            //set => Set(ref _numericValue2, Math.Round(value / step) * step);
            set
            {
                Set(ref _numericValue2, Math.Round(value / step) * step);
                Global.MainViewVM.CurrentHeight = _numericValue2.ToString();
            }
        }
        private double _openGLControl1Width;
        public double openGLControl1Width
        {
            get => _openGLControl1Width;
            set => Set(ref _openGLControl1Width, value);
        }
        private double _openGLControl1Height;
        public double openGLControl1Height
        {
            get => _openGLControl1Height;
            set => Set(ref _openGLControl1Height, value);
        }
        private bool _isAutoRotate = false;
        public bool IsAutoRotate
        {
            get => _isAutoRotate;
            set => Set(ref _isAutoRotate, value);
        }
        private string _portStatuslbl = "关闭";
        public string PortStatuslbl
        {
            get => _portStatuslbl;
            set => Set(ref _portStatuslbl, value);
        }
        private string _PrintStatuslbl = "停止";
        public string PrintStatuslbl
        {
            get => _PrintStatuslbl;
            set => Set(ref _PrintStatuslbl, value);
        }
        private Dispatcher dispatcher;
        private void PrintStatusView_Loaded_Execute()
        {
            try
            {
                Global.serialVM.GetDataEvent("");
                UploadInfo info = Utilities.hexStrToInfo("");
                Global.printer.X = info.xLocation / 1000;
                Global.printer.Y = info.yLocation / 1000;
                Global.printer.Z = info.zLocation / 1000;
                this.dispatcher.Invoke(new Action(() =>
                {
                    switch (info.sysStatus)
                    {
                        case "80":
                            if (Global.printer.printerState == 0)
                            {
                                break;
                            }
                            _mainWindowVM.timer.Start();
                            break;
                        case "82":
                            _mainWindowVM.tagTimer = 0;
                            if (Global.printer.printerState == 1)
                            {
                                break;
                            }
                            Global.printer.printerState = 1;
                            break;
                        case "83":
                            _mainWindowVM.timer.Stop();
                            if (Global.printer.printerState == 2)
                            {
                                break;
                            }
                            Global.printer.printerState = 2;
                            break;
                    }
                }));
            }
            catch (Exception ex)
            {
                Global.LogHelper.WriteLog(ex.Message);
            }
        }
    }
}
